package tugas.akhir.jason;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class laporan extends ListActivity{

	private DBAdapter mDbHelper;
	public final static String ID_EXTRA="tugas.akhir.jason._ID";
	private static final int DELETE_ID = Menu.FIRST + 1;
	EditText alamat=null;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.laporan);
		
		LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
		Toast.makeText(this, "GPS Aktif", Toast.LENGTH_SHORT).show();
		}else{
		showGPSDisabledAlertToUser();
		}

		if (AppStatus.getInstance(this).isOnline(this)) {
			Toast t = Toast.makeText(this,"Network Connectivity Available",8000);
	        t.show();
			
		}
		 mDbHelper = new DBAdapter(this);
	        mDbHelper.open();
	        fillData();
	    
	        registerForContextMenu(getListView());
	}
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }
	
	@Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteTitle(info.id);
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }

	
	public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this, 
                "ID Laporan: " + c.getString(1) + "\n" +
                "Alamat: " + c.getString(2) + "\n",
                Toast.LENGTH_LONG).show();        
    } 
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.option, menu);

		return(super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId()==R.id.add) {			
	        if (AppStatus.getInstance(this).isOnline(this)) {
	        	{ startActivity(new Intent(laporan.this, mapper.class)); }
	        	return(true);
	    }
	     else {  
	    	
	    	Toast t = Toast.makeText(this,"Tidak ada koneksi internet!!",8000);
	        t.show();
	        { startActivity(new Intent(laporan.this, mapper2.class)); }
	        return(true);
	}

		}
		if (item.getItemId()==R.id.upload) {
			{ startActivity(new Intent(laporan.this, login.class)); }
		}
		
		return(super.onOptionsItemSelected(item));
	}

	
	 private void fillData() {
	        // Get all of the notes from the database and create the item list
	        Cursor c = mDbHelper.getAll();
	        startManagingCursor(c);

	        String[] from = new String[] { DBAdapter.KEY_HEADERDATA};
	        int[] to = new int[] { R.id.text1};
	        
	        // Now create an array adapter and set it to display using our row
	        SimpleCursorAdapter notes =
	            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
	        setListAdapter(notes);
	    }
	 
	 private void showGPSDisabledAlertToUser(){
		 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		 alertDialogBuilder.setMessage("GPS belum aktif. Aktif kan GPS sebelum mulai asesmen!")
		 .setCancelable(false)
		 .setPositiveButton("Aktifkan GPS",
		 new DialogInterface.OnClickListener(){
		 public void onClick(DialogInterface dialog, int id){
		 Intent callGPSSettingIntent = new Intent(
		 android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		 startActivity(callGPSSettingIntent);
		 }
		 });
		 alertDialogBuilder.setNegativeButton("Kembali",
		 new DialogInterface.OnClickListener(){
		 public void onClick(DialogInterface dialog, int id){
		 dialog.cancel();
		 }
		 });
		 AlertDialog alert = alertDialogBuilder.create();
		 alert.show();
		 }

}